package com.proton.carepatchtemp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.application.VCare;
import com.proton.carepatchtemp.base.BaseActivity;
import com.proton.carepatchtemp.constant.Extras;
import com.proton.carepatchtemp.database.TestManager;
import com.proton.carepatchtemp.database.table.Device;
import com.proton.carepatchtemp.utils.AppUtils;
import com.proton.carepatchtemp.utils.eventbus.MainEvent;
import com.sinping.iosdialog.animation.BaseAnimatorSet;
import com.sinping.iosdialog.animation.BounceEnter.BounceTopEnter;
import com.sinping.iosdialog.animation.SlideExit.SlideBottomExit;
import com.sinping.iosdialog.dialog.listener.OnBtnClickL;
import com.sinping.iosdialog.dialog.widget.NormalDialog;

public class DeviceDetialActivity extends BaseActivity {

    private Device mDevice;
    private ImageButton mBtnBack;
    /**
     * 123
     */
    private TextView mDeviceNameUpper;
    /**
     * 2016.1.2
     */
    private TextView mTvLastUseTime;
    /**
     * 来自130511345687的共享
     */
    private TextView mTvDeviceFrom;
    /**
     * SF4GSD4G874G87S
     */
    private TextView mTvDeviceSn;
    /**
     * V-.-.-
     */
    private TextView mTvDeviceFirmwareVersion;
    /**
     * ------------
     */
    private TextView mTvDeviceMacAddress;
    private TextView mTvFirmwareNeedUpdate;
    private FrameLayout mFrlUpdateFirmware;


    private BaseAnimatorSet bas_in;
    private BaseAnimatorSet bas_out;

    private boolean needUpdate = false;

    @Override
    public void dealEvent(MainEvent event) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_detial);
        initView();
        setListener();
        if (getIntent().getExtras() == null) {
            finish();
        } else {
            mDevice = (Device) getIntent().getExtras().getSerializable(Extras.DEVICE_DETIAL);
        }

        if (AppUtils.compareVersion(mDevice.getVersion(), "1.0.2") >= 0) {
            if (null != VCare.get().getFirmWare() && AppUtils.compareVersion(mDevice.getVersion(), VCare.get().getFirmWare().getVersion()) < 0) {
                needUpdate = true;
                mTvFirmwareNeedUpdate.setVisibility(View.VISIBLE);
                mTvFirmwareNeedUpdate.setText("1");
                bas_in = new BounceTopEnter();
                bas_out = new SlideBottomExit();
            }
        }


        mTvDeviceFrom.setText(mDevice.getCreatorname());
        mTvDeviceSn.setText(mDevice.getSn());
        mTvLastUseTime.setText(mDevice.getHint());
        mDeviceNameUpper.setText(mDevice.getBtaddress().substring(12));
        mTvDeviceFirmwareVersion.setText(mDevice.getVersion());
        mTvDeviceMacAddress.setText(mDevice.getBtaddress());
    }

    @Override
    protected void setListener() {
        mBtnBack.setOnClickListener(v -> {
            finish();
        });

        mFrlUpdateFirmware.setOnClickListener(view -> {
            if (needUpdate && !TestManager.container.isEmpty()) {
                final NormalDialog dialog = new NormalDialog(DeviceDetialActivity.this);
                dialog.setCancelable(false);
                dialog.setCanceledOnTouchOutside(false);
                dialog.content(getString(R.string.string_update_fireware_warm))//
                        .btnNum(1)
                        .btnText(getString(R.string.string_i_konw))//
                        .showAnim(bas_in)//
                        .dismissAnim(bas_out)//
                        .show();

                //                    finish();
                dialog.setOnBtnClickL((OnBtnClickL) dialog::dismiss);
                return;
            }
            Intent goUpdateDetial = new Intent(DeviceDetialActivity.this, DeviceUpdateMsgActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable(Extras.DEVICE_DETIAL, mDevice);
            goUpdateDetial.putExtras(bundle);
            startActivity(goUpdateDetial);
        });
    }

    @Override
    public void onEventMainThread(MainEvent event) {

    }

    private void initView() {
        mBtnBack = findViewById(R.id.btn_back);
        mDeviceNameUpper = findViewById(R.id.device_name_upper);
        mTvLastUseTime = findViewById(R.id.tv_last_use_time);
        mTvDeviceFrom = findViewById(R.id.tv_device_from);
        mTvDeviceSn = findViewById(R.id.tv_device_sn);
        mTvDeviceFirmwareVersion = findViewById(R.id.tv_device_firmware_version);
        mTvDeviceMacAddress = findViewById(R.id.tv_device_mac_address);
        mTvFirmwareNeedUpdate = findViewById(R.id.tv_firmware_need_update);
        mFrlUpdateFirmware = findViewById(R.id.frl_update_firmware);
    }
}
